package com.cnfilm.web.film;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.cnfilm.utils.CacheManager;
import com.cnfilm.utils.DateUtils;
import com.cnfilm.utils.RssUtils;
import com.cnfilm.utils.StringUtils;
import com.cnfilm.vo.FilmAllVO;
import com.cnfilm.vo.FilmVO;
import com.cnfilm.vo.ScoreVO;
import com.cnfilm.vo.VideoAllVO;
import com.cnfilm.vo.VideoVO;
import com.cnfilm.web.category.Category;
import com.cnfilm.web.code.Code;
import com.cnfilm.web.gbook.Gbook;
import com.cnfilm.web.tags.Tags;
import com.cnfilm.web.video.Video;
import com.cnfilm.web.website.Website;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * 影片管理
 * @author jiang.li
 */
public class FilmController extends Controller {
	
	/**
	 * 根据tag名称获取相关的电影
	 */
	public void tagfilm(){
		/**Tag名称**/
		String tname = "";
		try {
			tname = getAttr("tname")==null?"电影": URLDecoder.decode(getAttrForStr("tname"),"UTF-8");
			tname = StringUtils.getSearchWord(tname);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		setAttr("tname", tname);
		List<Film> gkphList = Film.dao.find("select A.*,B.url as curl,B.cname as cname from film A,category B,tags C where A.cid=B.cid and A.tid = C.tid and C.tname=? order by A.fid desc",tname);
		List<FilmVO> dataList = new ArrayList<FilmVO>();
		FilmVO fvo = null;
		for(Film f:gkphList){
			fvo = new FilmVO();
			fvo.setCurl(f.getStr("curl"));
			fvo.setUrl(f.getStr("url"));
			fvo.setFname(f.getStr("fname"));
			fvo.setCname(f.getStr("cname"));
			fvo.setImgurl(f.getStr("imgurl"));
			fvo.setStarring(f.getStr("starring"));
			fvo.setDirector(f.getStr("director"));
			fvo.setTime(f.getStr("addtime").substring(5, 10));
			fvo.setTags(f.getStr("tags"));
			dataList.add(fvo);
		}
		setAttr("filmList", dataList);
		/**导航菜单**/
		List<Category> menuList = Category.getAllCategory();
		setAttr("menuList", menuList);
		
		/**点击排行**/
		List<FilmVO> seefvolist =  new ArrayList<FilmVO>();
		FilmVO seefvo = null;
		String sql = "select A.*,B.url as curl,B.cname as cname from film A,category B where A.cid=B.cid order by A.addtime desc limit 25";
		List<Film> djphList = Film.dao.find(sql);
		for(Film f:djphList){
			seefvo = new FilmVO();
			seefvo.setCurl(f.getStr("curl"));
			seefvo.setUrl(f.getStr("url"));
			seefvo.setFname(f.getStr("fname"));
			seefvo.setTime(f.getStr("addtime").substring(5, 10));
			seefvolist.add(seefvo);
		}
		setAttr("djphList", seefvolist);
		
		/**全站点击排行**/
		setAttr("qzdjphList", getListDianjiList());
		
		/**搜索下面的文章**/
		setAttr("searchList", getListSearchList(menuList));
		/**获取网站配置**/
		HashMap<String,String> map = Website.getConfig();
		setAttr("config", map);
		/**地区分类**/
		List<Code> areaList = Code.getCodeByType("area");
		setAttr("areaList", areaList);
		render("/view/tag.jsp");
	}
	
	/**
	 * 获取所有tag
	 */
	public void tags(){
		/**导航菜单**/
		List<Category> menuList = Category.getAllCategory();
		setAttr("menuList", menuList);
		
		/**点击排行**/
		List<FilmVO> seefvolist =  new ArrayList<FilmVO>();
		FilmVO seefvo = null;
		String sql = "select A.*,B.url as curl,B.cname as cname from film A,category B where A.cid=B.cid order by A.addtime desc limit 25";
		List<Film> djphList = Film.dao.find(sql);
		for(Film f:djphList){
			seefvo = new FilmVO();
			seefvo.setCurl(f.getStr("curl"));
			seefvo.setUrl(f.getStr("url"));
			seefvo.setFname(f.getStr("fname"));
			seefvo.setTime(f.getStr("addtime").substring(5, 10));
			seefvolist.add(seefvo);
		}
		setAttr("djphList", seefvolist);
		
		/**全站点击排行**/
		setAttr("qzdjphList", getListDianjiList());
		
		/**搜索下面的文章**/
		setAttr("searchList", getListSearchList(menuList));
		/**获取网站配置**/
		HashMap<String,String> map = Website.getConfig();
		setAttr("config", map);
		/**地区分类**/
		List<Code> areaList = Code.getCodeByType("area");
		setAttr("areaList", areaList);
		
		/**获取所有tags数据**/
		List<Tags> allTagsList = Tags.getAllTags();
		setAttr("tagList", allTagsList);
		render("/view/tags.jsp");
	}
	
	
	
	/**
	 * 网站html格式地图
	 */
	public void sitemap(){
		/**导航菜单**/
		List<Category> menuList = Category.getAllCategory();
		/**首页下的总分类详细**/
		List<FilmAllVO> filmAllList = getFilmSeeAllList(menuList);
		setAttr("filmList", filmAllList);
		render("/view/sitemap.jsp");
	}
	
	
	/**
	 * 网站地图下的总分类详细
	 * @param menuList
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private List<FilmAllVO> getFilmSeeAllList(List<Category> menuList){
		List<FilmAllVO> filmAllList = new ArrayList<FilmAllVO>();
		String key = "cache_getFilmSeeAllList_2013";
		boolean hasCache = CacheManager.hasCache(key);
		if(hasCache){
			filmAllList = (List<FilmAllVO>)CacheManager.getCache(key);
		}else{
			FilmAllVO vo = null;
			List<Film> flist = null;
			List<FilmVO> fvolist = null;
			FilmVO fvo = null;
			for(Category c:menuList){
				fvolist = new ArrayList<FilmVO>();
				vo = new FilmAllVO();
				vo.setCname(c.getStr("cname"));
				vo.setCurl(c.getStr("url"));
				flist = Film.dao.find("select A.*,B.url as curl,B.cname as cname from film A,category B where A.cid=B.cid and A.cid=? order by A.addtime desc limit 100",c.get("cid"));
				long fnum = Db.queryLong("select count(1) from film where cid = ? ",c.get("cid"));
				vo.setFnum(fnum);
				for(Film f:flist){
					fvo = new FilmVO();
					fvo.setCurl(f.getStr("curl"));
					fvo.setUrl(f.getStr("url"));
					fvo.setFname(f.getStr("fname"));
					List<Video> vList = Video.dao.find("select * from video where fid=?",f.get("fid"));
					List<VideoVO> voList = new ArrayList<VideoVO>();
					VideoVO vvo = null;
					for(Video v:vList){
						vvo = new VideoVO();
						vvo.setVid(v.getInt("vid"));
						vvo.setTitle(v.getStr("vtitle"));
						vvo.setShow(v.getStr("vurl"));
						voList.add(vvo);
					}
					fvo.setList(voList);
					fvolist.add(fvo);
				}
				vo.setList(fvolist);
				filmAllList.add(vo);
			}
			CacheManager.putCache(key, filmAllList);
		}
		return filmAllList;
	}
	
	/**
	 * 获取分类下的数据（分页）
	 */
	public void list(){
		/**请求的分类url**/
		String url = getAttrForStr("url");
		/**获取分类**/
		Category category = Category.dao.findFirst("select * from category where url=?", url);
		if(category!=null){
			/**分页页数**/
			int pageNumber = getAttr("page")==null?1: Integer.parseInt(getAttrForStr("page"));
			StringBuffer where = new StringBuffer("from film where 1=1 ");
			where.append("and cid = ");
			where.append(category.getInt("cid"));
			where.append(" ");
			where.append("order by addtime desc");
			Page<Film> filmList = Film.dao.paginate(pageNumber, 16, "select *", where.toString());
			List<Film> gkphList = filmList.getList();
			List<FilmVO> dataList = new ArrayList<FilmVO>();
			FilmVO fvo = null;
			for(Film f:gkphList){
				fvo = new FilmVO();
				fvo.setCurl(f.getStr("curl"));
				fvo.setUrl(f.getStr("url"));
				fvo.setFname(f.getStr("fname"));
				fvo.setImgurl(f.getStr("imgurl"));
				fvo.setStarring(f.getStr("starring"));
				fvo.setDirector(f.getStr("director"));
				fvo.setTime(f.getStr("addtime").substring(5, 10));
				dataList.add(fvo);
			}
			setAttr("pageList", filmList);
			setAttr("dataList", dataList);
			setAttr("category", category);
			/**导航菜单**/
			List<Category> menuList = Category.getAllCategory();
			setAttr("menuList", menuList);
			
			/**点击排行**/
			List<FilmVO> seefvolist =  new ArrayList<FilmVO>();
			FilmVO seefvo = null;
			String sql = "select A.*,B.url as curl,B.cname as cname from film A,category B where A.cid=B.cid and A.cid=? order by A.addtime desc limit 25";
			List<Film> djphList = Film.dao.find(sql,category.get("cid"));
			for(Film f:djphList){
				seefvo = new FilmVO();
				seefvo.setCurl(f.getStr("curl"));
				seefvo.setUrl(f.getStr("url"));
				seefvo.setFname(f.getStr("fname"));
				seefvo.setTime(f.getStr("addtime").substring(5, 10));
				seefvolist.add(seefvo);
			}
			setAttr("djphList", seefvolist);
			
			/**全站点击排行**/
			setAttr("qzdjphList", getListDianjiList());
			
			/**搜索下面的文章**/
			setAttr("searchList", getNewListSearchList(category.getInt("cid")));
			/**地区分类**/
			List<Code> areaList = Code.getCodeByType("area");
			setAttr("areaList", areaList);
			render("/view/list.jsp");
		}else{
			getResponse().setStatus(404);
		}
	}
	
	/**
	 * 获取地区类型的数据（分页）
	 */
	public void area(){
		/**请求的地区**/
		String area = getAttrForStr("area");
		/**分页页数**/
		int pageNumber = getAttr("page")==null?1: Integer.parseInt(getAttrForStr("page"));
		StringBuffer where = new StringBuffer("from film A,category B where A.cid=B.cid and A.area = ? order by A.addtime desc");
		Page<Film> filmList = Film.dao.paginate(pageNumber, 16, "select A.*,B.url as curl,B.cname as cname", where.toString(),area);
		List<Film> gkphList = filmList.getList();
		List<FilmVO> dataList = new ArrayList<FilmVO>();
		FilmVO fvo = null;
		for(Film f:gkphList){
			fvo = new FilmVO();
			fvo.setCurl(f.getStr("curl"));
			fvo.setUrl(f.getStr("url"));
			fvo.setCname(f.getStr("cname"));
			fvo.setFname(f.getStr("fname"));
			fvo.setImgurl(f.getStr("imgurl"));
			fvo.setStarring(f.getStr("starring"));
			fvo.setDirector(f.getStr("director"));
			fvo.setTime(f.getStr("addtime").substring(5, 10));
			dataList.add(fvo);
		}
		setAttr("pageList", filmList);
		setAttr("dataList", dataList);
		/**导航菜单**/
		List<Category> menuList = Category.getAllCategory();
		setAttr("menuList", menuList);
		
		/**点击排行**/
		List<FilmVO> seefvolist =  new ArrayList<FilmVO>();
		FilmVO seefvo = null;
		String sql = "select A.*,B.url as curl,B.cname as cname from film A,category B where A.cid=B.cid and A.area = ? order by A.addtime desc limit 25";
		List<Film> djphList = Film.dao.find(sql,area);
		for(Film f:djphList){
			seefvo = new FilmVO();
			seefvo.setCurl(f.getStr("curl"));
			seefvo.setUrl(f.getStr("url"));
			seefvo.setFname(f.getStr("fname"));
			seefvo.setTime(f.getStr("addtime").substring(5, 10));
			seefvolist.add(seefvo);
		}
		setAttr("djphList", seefvolist);
		
		/**全站点击排行**/
		setAttr("qzdjphList", getListDianjiList());
		
		/**搜索下面的文章**/
		setAttr("searchList", getListSearchList(menuList));
		
		/**地区分类**/
		List<Code> areaList = Code.getCodeByType("area");
		setAttr("areaList", areaList);
		setAttr("area", area);
		/**获取网站配置**/
		HashMap<String,String> map = Website.getConfig();
		setAttr("config", map);
		/**获取地区名称**/
		String areaName = Code.getNameByCodeAndType("area", area);
		setAttr("areaName", areaName);
		render("/view/area.jsp");
	}
	
	
	/**
	 * 获取最新的电影数据（分页）
	 */
	public void news(){
		/**分页页数**/
		int pageNumber = getAttr("page")==null?1: Integer.parseInt(getAttrForStr("page"));
		StringBuffer where = new StringBuffer("from film A,category B where A.cid=B.cid order by A.fid desc");
		Page<Film> filmList = Film.dao.paginate(pageNumber, 50, "select A.*,B.url as curl,B.cname as cname ", where.toString());
		List<Film> gkphList = filmList.getList();
		List<FilmVO> dataList = new ArrayList<FilmVO>();
		FilmVO fvo = null;
		for(Film f:gkphList){
			fvo = new FilmVO();
			fvo.setCurl(f.getStr("curl"));
			fvo.setUrl(f.getStr("url"));
			fvo.setFname(f.getStr("fname"));
			fvo.setCname(f.getStr("cname"));
			fvo.setImgurl(f.getStr("imgurl"));
			fvo.setStarring(f.getStr("starring"));
			fvo.setDirector(f.getStr("director"));
			fvo.setTime(f.getStr("addtime").substring(5, 10));
			fvo.setTags(f.getStr("tags"));
			dataList.add(fvo);
		}
		setAttr("pageList", filmList);
		setAttr("dataList", dataList);
		/**导航菜单**/
		List<Category> menuList = Category.getAllCategory();
		setAttr("menuList", menuList);
		
		/**点击排行**/
		List<FilmVO> seefvolist =  new ArrayList<FilmVO>();
		FilmVO seefvo = null;
		String sql = "select A.*,B.url as curl,B.cname as cname from film A,category B where A.cid=B.cid order by A.addtime desc limit 25";
		List<Film> djphList = Film.dao.find(sql);
		for(Film f:djphList){
			seefvo = new FilmVO();
			seefvo.setCurl(f.getStr("curl"));
			seefvo.setUrl(f.getStr("url"));
			seefvo.setFname(f.getStr("fname"));
			seefvo.setTime(f.getStr("addtime").substring(5, 10));
			seefvolist.add(seefvo);
		}
		setAttr("djphList", seefvolist);
		
		/**全站点击排行**/
		setAttr("qzdjphList", getListDianjiList());
		
		/**搜索下面的文章**/
		setAttr("searchList", getListSearchList(menuList));
		/**获取网站配置**/
		HashMap<String,String> map = Website.getConfig();
		setAttr("config", map);
		/**地区分类**/
		List<Code> areaList = Code.getCodeByType("area");
		setAttr("areaList", areaList);
		render("/view/news.jsp");
	}
	
	public void search(){
		String word = getPara("word");
		word = StringUtils.getSearchWord(word);
		setAttr("word", word);
		String sword = "%"+word+"%";
		/**分页页数**/
		List<Film> gkphList = Film.dao.find("select A.*,B.url as curl,B.cname as cname from film A,category B where A.cid=B.cid and (A.fname like ? or A.nickname like ?) order by A.clicknum desc",sword,sword);
		List<FilmVO> dataList = new ArrayList<FilmVO>();
		FilmVO fvo = null;
		for(Film f:gkphList){
			fvo = new FilmVO();
			fvo.setCurl(f.getStr("curl"));
			fvo.setCname(f.getStr("cname"));
			fvo.setUrl(f.getStr("url"));
			fvo.setFname(f.getStr("fname"));
			fvo.setImgurl(f.getStr("imgurl"));
			fvo.setStarring(f.getStr("starring"));
			fvo.setDirector(f.getStr("director"));
			fvo.setTime(f.getStr("addtime").substring(5, 10));
			dataList.add(fvo);
		}
		setAttr("dataList", dataList);
		setAttr("num", dataList.size());
		/**导航菜单**/
		List<Category> menuList = Category.getAllCategory();
		setAttr("menuList", menuList);
		
		/**全站点击排行**/
		setAttr("qzdjphList", getListDianjiList());
		
		/**搜索下面的文章**/
		setAttr("searchList", getListSearchList(menuList));
		/**获取网站配置**/
		HashMap<String,String> map = Website.getConfig();
		setAttr("config", map);
		/**地区分类**/
		List<Code> areaList = Code.getCodeByType("area");
		setAttr("areaList", areaList);
		render("/view/search.jsp");
		
	}
	
	/**
	 * RSS服务
	 */
	public void rss(){
		/**获取网站配置**/
		HashMap<String,String> map = Website.getConfig();
		/**最近更新**/
		List<Film> filmList = Film.dao.find("select A.*,B.url as curl,B.cname as cname from film A,category B where A.cid=B.cid order by A.addtime desc limit 200");
		String rssText = RssUtils.getRssString(filmList,map);
		this.renderText(rssText, "application/xml");
	}
	
	/**
	 * 获取单个影片信息
	 */
	public void getfilm(){
		/**获取电影数据**/
		String furl = getAttrForStr("furl");
		Record record = Db.findFirst("select * from film where url = ?",furl);
		if(record!=null){
			int num = record.getInt("clicknum");
			record.set("clicknum", num+1);
			Db.update("film", "fid", record);
			setAttr("film", record);
			Category category = Category.dao.findFirst("select * from category where cid = ?", record.getInt("cid"));
			if(category!=null){
				setAttr("category", category);
				/**导航菜单**/
				List<Category> menuList = Category.getAllCategory();
				setAttr("menuList", menuList);
				/**获取用户还喜欢的电影数据**/
				List<Film> likeList = Film.dao.find("select A.*,B.url as curl,B.cname as cname from film A,category B where A.cid=B.cid and A.cid=? order by A.clicknum desc limit 7",record.getInt("cid"));
				setAttr("likeList", likeList);
				/**获取影片播放源数据**/
				List<VideoAllVO> videoAllList = new ArrayList<VideoAllVO>();
				List<Code> codeList = Code.getCodeByType("vtype");
				VideoAllVO videoallCO = null;
				for(Code c:codeList){
					videoallCO = new VideoAllVO();
					videoallCO.setCode(c.getStr("code"));
					videoallCO.setImg(c.getStr("img"));
					videoallCO.setName(c.getStr("name"));
					List<Video> videoList = Video.dao.find("select * from video where fid=? and vtype=? order by vid asc",record.getInt("fid"), c.getStr("code"));
					if(videoList!=null && videoList.size()>0){
						List<VideoVO> videoVOList = new ArrayList<VideoVO>();
						VideoVO videoVO = null;
						for(Video v:videoList){
							videoVO = new VideoVO();
							videoVO.setVid(v.getInt("vid"));
							videoVO.setTitle(v.getStr("vtitle"));
							videoVO.setShow(v.getStr("vshow"));
							videoVOList.add(videoVO);
						}
						videoallCO.setList(videoVOList);
						videoAllList.add(videoallCO);
					}
				}
				setAttr("videoAllList", videoAllList);
				
				/**搜索下面的文章**/
				setAttr("searchList", getListSearchList(menuList));
				/**获取网站配置**/
				HashMap<String,String> map = Website.getConfig();
				setAttr("config", map);
				/**地区分类**/
				List<Code> areaList = Code.getCodeByType("area");
				setAttr("areaList", areaList);
				/**获取标签**/
				String tid = record.get("tid")==null?"":record.getStr("tid");
				if(!tid.equals("")){
					Tags tag = Tags.dao.findFirst("select * from tags where tid=?", tid);
					setAttr("tag", tag);
				}
				render("/view/article.jsp");
			}else{
				getResponse().setStatus(404);
			}
		}else{
			getResponse().setStatus(404);
		}
	}
	
	/**
	 * 获取播放源信息
	 */
	public void getvideo(){
		/**获取电影数据**/
		String furl = getAttrForStr("furl");
		Record frecord = Db.findFirst("select * from film where url = ?",furl);
		if(frecord!=null){
			setAttr("film", frecord);
			/**获取分类**/
			Category category = Category.dao.findFirst("select * from category where cid = ?", frecord.getInt("cid"));
			setAttr("category", category);
			/**获取播放源**/
			int vid = getAttr("vid")==null?1: Integer.parseInt(getAttrForStr("vid"));
			Record record = Db.findFirst("select * from video where vid = ?",vid);
			int num = record.getInt("clicknum");
			record.set("clicknum", num+1);
			Db.update("video", "vid", record);
			setAttr("video", record);
			/**导航菜单**/
			List<Category> menuList = Category.getAllCategory();
			setAttr("menuList", menuList);
			/**获取用户还喜欢的电影数据**/
			List<Film> likeList = Film.dao.find("select A.*,B.url as curl,B.cname as cname from film A,category B where A.cid=B.cid and A.cid=? order by A.clicknum desc limit 7",frecord.getInt("cid"));
			setAttr("likeList", likeList);
			/**获取播放模板**/
			String content = Code.getContentByCodeAndType("vtype", record.getStr("vtype"));
			content = content.replace("bdvcd", record.getStr("vurl"));
			setAttr("player", content);
			
			/**获取上下级播放源**/
			Record shang = Db.findFirst("select * from video where vid < ? order by vid asc limit 0,1",vid);
			setAttr("shang", shang);
			Record xia = Db.findFirst("select * from video where vid > ? order by vid desc limit 0,1",vid);
			setAttr("xia", xia);
			/**获取网站配置**/
			HashMap<String,String> map = Website.getConfig();
			setAttr("config", map);
			render("/view/play.jsp");
		}else{
			getResponse().setStatus(404);
		}
	}

	/**
	 * 评分
	 */
	public void score(){
		String furl = getAttrForStr("furl");
		int score = getPara("score")==null?5: Integer.parseInt(getPara("score"));
		Record record = Db.findFirst("select * from film where url = ?",furl);
		int pnum = record.getInt("pnum")+1;
		int psum = record.getInt("psum")+score;
		double pabs = div(psum,pnum,2);
		record.set("pnum", pnum);
		record.set("pabs", pabs);
		record.set("psum", psum);
		Db.update("film", "fid", record);
		ScoreVO svo = new ScoreVO();
		svo.setPabs(pabs);
		svo.setPnum(pnum);
		svo.setPsum(psum);
		renderJson(svo);
	}
	
	/**
	* 提供（相对）精确的除法运算。当发生除不尽的情况时，由scale参数指
	* 定精度，以后的数字四舍五入。
	* @param v1 被除数
	* @param v2 除数
	* @param scale 表示表示需要精确到小数点以后几位。
	* @return 两个参数的商
	*/
	public static double div(double v1, double v2, int scale) {
	   if (scale < 0) {
	    throw new IllegalArgumentException(
	      "The scale must be a positive integer or zero");
	   }
	   BigDecimal b1 = new BigDecimal(Double.toString(v1));
	   BigDecimal b2 = new BigDecimal(Double.toString(v2));
	   return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
	}
	
	/**
	 * 获取留言数据
	 */
	public void gbook(){
		/**获取网站配置**/
		HashMap<String,String> map = Website.getConfig();
		setAttr("config", map);
		/**导航菜单**/
		List<Category> menuList = Category.getAllCategory();
		setAttr("menuList", menuList);
		int pageNumber = getAttr("page")==null?1: Integer.parseInt(getAttrForStr("page"));
		Page<Gbook> gbookList = Gbook.dao.paginate(pageNumber, 20, "select *", "from gbook where replay is not null order by id desc");
		setAttr("pageList", gbookList);
		/**地区分类**/
		List<Code> areaList = Code.getCodeByType("area");
		setAttr("areaList", areaList);
		/**搜索下面的文章**/
		setAttr("searchList", getListSearchList(menuList));
		render("/view/gbook.jsp");
	}
	
	/**
	 * 添加留言
	 */
	public void addgbook(){
		String nickname = getPara("nickname")==null?"贴心网友":StringUtils.getSearchWord(getPara("nickname"));
		String content = getPara("content")==null?"木有留言内容":StringUtils.getSearchWord(getPara("content"));
		Record c = new Record();
		c.set("addtime", DateUtils.getNowTime(DateUtils.DATE_FULL_STR));
		c.set("nickname", nickname);
		c.set("content", content);
		boolean isok = Db.save("gbook", c);
		if(isok){
			renderText("留言成功，请耐心等待管理员回复！");
		}else{
			renderText("留言失败！");
		}
	}
	
	/**
	 * 百度影音资源
	 */
	public void bdhd(){
		/**分页页数**/
		int pageNumber = getAttr("page")==null?1: Integer.parseInt(getAttrForStr("page"));
		StringBuffer where = new StringBuffer("from video A,film B, category C where A.fid=B.fid and B.cid = C.cid and A.vtype='baidu' order by A.addtime desc");
		Page<Film> filmList = Film.dao.paginate(pageNumber, 50, "select A.*,B.url,B.fname as fname,C.url as curl,C.cname ", where.toString());
		List<Film> gkphList = filmList.getList();
		List<FilmVO> dataList = new ArrayList<FilmVO>();
		FilmVO fvo = null;
		for(Film f:gkphList){
			fvo = new FilmVO();
			fvo.setVid(f.getInt("vid"));
			fvo.setVurl(f.getStr("vurl"));
			fvo.setUrl(f.getStr("url"));
			fvo.setFname(f.getStr("fname"));
			fvo.setVtitle(f.getStr("vtitle"));
			fvo.setCurl(f.getStr("curl"));
			fvo.setCname(f.getStr("cname"));
			fvo.setTime(f.getStr("addtime").substring(5, 10));
			dataList.add(fvo);
		}
		setAttr("pageList", filmList);
		setAttr("dataList", dataList);
		/**导航菜单**/
		List<Category> menuList = Category.getAllCategory();
		setAttr("menuList", menuList);
		
		/**点击排行**/
		List<FilmVO> seefvolist =  new ArrayList<FilmVO>();
		FilmVO seefvo = null;
		String sql = "select A.*,B.url as curl,B.cname as cname from film A,category B where A.cid=B.cid order by A.addtime desc limit 25";
		List<Film> djphList = Film.dao.find(sql);
		for(Film f:djphList){
			seefvo = new FilmVO();
			seefvo.setCurl(f.getStr("curl"));
			seefvo.setUrl(f.getStr("url"));
			seefvo.setFname(f.getStr("fname"));
			seefvo.setTime(f.getStr("addtime").substring(5, 10));
			seefvolist.add(seefvo);
		}
		setAttr("djphList", seefvolist);
		
		/**全站点击排行**/
		setAttr("qzdjphList", getListDianjiList());
		
		/**搜索下面的文章**/
		setAttr("searchList", getListSearchList(menuList));
		/**获取网站配置**/
		HashMap<String,String> map = Website.getConfig();
		setAttr("config", map);
		/**地区分类**/
		List<Code> areaList = Code.getCodeByType("area");
		setAttr("areaList", areaList);
		render("/view/bdhd.jsp");
	}
	
	/**
	 * 全站点击排行
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private List<Film> getListDianjiList(){
		String lasttime = DateUtils.getLastTime(new Date(),-30);
		List<Film> qzdjphList = new ArrayList<Film>();
		String key = "cache_getListDianjiList_2013";
		boolean hasCache = CacheManager.hasCache(key);
		if(hasCache){
			qzdjphList = (List<Film>)CacheManager.getCache(key);
		}else{
			qzdjphList = Film.dao.find("select A.*,B.url as curl,B.cname as cname from film A,category B where A.cid=B.cid and A.addtime>=? order by A.clicknum desc limit 25",lasttime);
			CacheManager.putCache(key, qzdjphList);
		}
		return qzdjphList;
		
	}
	
	
	/**
	 * 搜索下面的文章
	 * @param menuList
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private List<Film> getListSearchList(List<Category> menuList){
		String lasttime = DateUtils.getLastTime(new Date(),-30);
		List<Film> searchList = new ArrayList<Film>();
		String key = "cache_getListSearchList_2013";
		boolean hasCache = CacheManager.hasCache(key);
		if(hasCache){
			searchList = (List<Film>)CacheManager.getCache(key);
		}else{
			Film djpht = null;
			int num = 1;
			for(Category c:menuList){
				if(num<=8){
					djpht = Film.dao.findFirst("select A.*,B.url as curl,B.cname as cname from film A,category B where A.cid=B.cid and A.cid=? and A.addtime>=? order by A.clicknum desc limit 1",c.getInt("cid"),lasttime);
					if(djpht==null){
						continue;
					}
					searchList.add(djpht);
				}else{
					break;
				}
				num++;
			}
			CacheManager.putCache(key, searchList);
		}
		return searchList;
	}
	
	/**
	 * 搜索下面的文章List
	 * @param menuList
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private List<Film> getNewListSearchList(int cid){
		String lasttime = DateUtils.getLastTime(new Date(),-30);
		List<Film> searchList = new ArrayList<Film>();
		String key = "cache_getNewListSearchList_2013_"+cid;
		boolean hasCache = CacheManager.hasCache(key);
		if(hasCache){
			searchList = (List<Film>)CacheManager.getCache(key);
		}else{
			searchList = Film.dao.find("select A.*,B.url as curl,B.cname as cname from film A,category B where A.cid=B.cid and A.cid=? and A.addtime>=? order by A.clicknum desc limit 7",cid,lasttime);
			CacheManager.putCache(key, searchList);
		}
		return searchList;
	}
}


